﻿using System.Collections.Generic;

namespace Marmat.Forms
{
    public partial class DataGrid
    {
        public string GetView()
        {
            var Items = new List<ViewColumn>();
            int @int = 0;
            // For Each GridBand As DevExpress.XtraGrid.Views.BandedGrid.GridBand In Me.BaseAdvBandedGridView.Bands
            // If GridBand.Visible Then
            // Dim Item As New ViewColumn
            // Item.Name = GridBand.Name
            // Item.Width = GridBand.Width
            // Items.Add(Item)
            // int += 1
            // End If
            // Next

            foreach (DevExpress.XtraGrid.Columns.GridColumn GridBand in this.BaseGridView.VisibleColumns)
            {
                if (GridBand.Visible)
                {
                    var Item = new ViewColumn();
                    Item.Name = GridBand.Name;
                    Item.Width = GridBand.Width;
                    Items.Add(Item);
                    @int += 1;
                }
            }

            string info = Newtonsoft.Json.JsonConvert.SerializeObject(Items);
            return info;
        }

        public void SetView(string Info)
        {
            var Items = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ViewColumn>>(Info);
            var zd = new Dictionary<string, ViewColumn>();
            foreach (ViewColumn Item in Items)
                zd.Add(Item.Name, Item);
            // For Each GridBand As DevExpress.XtraGrid.Views.BandedGrid.GridBand In Me.BaseAdvBandedGridView.Bands
            // GridBand.Visible = zd.ContainsKey(GridBand.Name)
            // Next
            // Dim int As Integer = 0
            // For Each item As BDataGrid.ViewColumn In Items
            // Dim GridBand As DevExpress.XtraGrid.Views.BandedGrid.GridBand = Me.BaseAdvBandedGridView.Bands(item.Name)
            // If GridBand IsNot Nothing Then
            // GridBand.Width = item.Width
            // Me.BaseAdvBandedGridView.Bands.MoveTo(int, GridBand)
            // int += 1
            // End If
            // Next
            foreach (DevExpress.XtraGrid.Columns.GridColumn GridBand in this.BaseGridView.Columns)
                GridBand.Visible = zd.ContainsKey(GridBand.Name);
            int @int = 0;
            foreach (ViewColumn item in Items)
            {
                var GridBand = this.BaseGridView.Columns[item.Name];
                if (GridBand is object)
                {
                    GridBand.Width = item.Width;
                    GridBand.VisibleIndex = @int;
                    @int += 1;
                }
            }
        }

        public class ViewColumn
        {
            public string Name { get; set; }
            public int Width { get; set; }
        }
    }
}